/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver;

import android.content.Context;
import android.content.res.Resources.NotFoundException;

import com.hercules.android.receiver.app.DisplayApplicationActivity;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Helper class for this component.
 * <p>
 * AndroidAssembly1 added new methods and changed visibility
 * <p>
 * <b>Thread safety:</b> This class is immutable and so thread safe.
 *
 * @author 7realm
 * @author 7realm
 * @version 1.0
 */
public final class ReceiverUtil {
    /**
     * This private constructor prevents to create a new instance.
     */
    private ReceiverUtil() {
        // empty
    }

    /**
     * Checks whether the given property value is null.
     *
     * @param arg the value to check
     * @param name the name of the property to check
     * @throws IllegalStateException if the given property value is null
     */
    static void checkState(Object arg, String name) {
        if (arg == null) {
            throw new IllegalStateException("Property '" + name + "' should not be null.");
        }
    }

    /**
     * Loads properties from resource ID.
     *
     * @param context the context used to load data from file
     * @param id the resource ID
     * @return loaded properties
     * @throws IOException if I/O error occurs
     * @throws DisplayApplicationActivity.ConfigurationException if reflection related error occurs
     */
    public static Properties loadProperties(Context context, int id) throws IOException,
        DisplayApplicationActivity.ConfigurationException {
        Properties properties = new Properties();
        InputStream input = null;
        try {
            input = context.getResources().openRawResource(id);
            properties.load(input);
        } catch (NotFoundException e) {
            throw new DisplayApplicationActivity.ConfigurationException(
                "Found no configured resource for resource ID '" + id + "'.", e);
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
        return properties;
    }

    /**
     * Get application name from URL.
     *
     * @param url the URL to parse
     * @return the application name as last token after back slash
     */
    public static String getAppNameFromUrl(String url) {
        return url.substring(url.lastIndexOf('/') + 1);
    }
    
    public static Map<String,Object> getAppParams(String paramsString) {
    	Map<String,Object> result = new HashMap<String, Object>();
    	String[] pairs = paramsString.split("&");
    	for (String pair : pairs) {
    		String[] param = pair.split("=");
    		result.put(param[0], param[1]);
    	}
        return result;
    }
}